/**
 * com.pas.iam.account.resource.NewJDialog.java
 * Copyright (c) 2009 Pas corporation All Rights Reserved.
 */
package com.web.server.common.dao;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 分页对象
 * <p>
 * 该类实现了分页查询结果和分页信息的封装。
 * 
 * @author  刘红军
 * @version Ver 1.0 2008-03-06 新建
 * @since   PASIAM Ver1.0
 */
public class PageListData implements java.io.Serializable
{
    /**
     * 保存分页数据对象
     * 
     * @since PASIAM Ver1.0
     */
    private List dataArray = null;

    /**
     * 记录总数
     * 
     * @since PASIAM Ver1.0
     */
    private int count = -1;

    /**
     * 每页显示记录个数
     * 
     * @since PASIAM Ver1.0
     */
    private int pageSize = 10;

    /**
     * 总页数
     * 
     * @since PASIAM Ver1.0
     */
    private int pageCount = 0;

    /**
     * 需要请求的页码
     * 
     * @since PASIAM Ver1.0
     */
    private int pageNeeded = 1;

    /**
     * 分页标示 1-分页 0-不分页
     * 
     * @since PASIAM Ver1.0
     */
    private int flag = 1;

    /**
     * 数据过滤 1-过滤 0-不过滤
     * 
     * @since PASIAM Ver1.0
     */
    private int countFilter = 1;

    /**
     * 获得分页操作栏
     * <p>
     * 在jsp页面中显示的分页操作，栏必需被包含在某个Form之中
     * 
     * @return String 分栏字符串
     * @author 刘红军
     * @since  PASIAM Ver1.0
     */
    public String getFooter(){
        StringBuffer str = new StringBuffer("");
        int prev = pageNeeded - 1;
        int next = pageNeeded + 1;

        // 添加操作分页栏的js脚本
        /*str.append("<script> ");
        str.append("function topagesubmit(pagestr){");
        str.append("with (document.forms[0]){");
        str.append("pageNeeded.value=pagestr;");
        str.append("submit();");
        str.append("}");
        str.append("}");
        str.append("</script>");*/

        // 如果当前页大于第一页，显示上一页和首页
        if (pageNeeded > 1){
            str.append("<a href='#' onclick='pageNeeded.value=" + 1 + "; submit(); '>首页</a> ");
            str.append("&nbsp;<a href='#' onclick='pageNeeded.value=" + prev + "; submit();'>上一页</a>");
        } else{
            str.append("<a href='#' disabled='true'>首页</a>");
            str.append("&nbsp;<a href='#' disabled='true'>上一页</a>");
        }

        // 如果当前页小于最后一页，显示下一页
        if (pageNeeded < pageCount){
            str.append("&nbsp;<a href='#' onclick=' pageNeeded.value=" + next + "; submit();'>下一页</a>");

        } else{
            str.append("&nbsp;<a href='#' disabled='true'>下一页</a>");
        }

        // 如果当前页不是最后一页，而且当前页大于第一页
        if (pageCount > 1 && pageNeeded != pageCount){
            str.append("&nbsp;<a href='#' onclick='pageNeeded.value=" + pageCount + "; submit();'>末页</a>");
        } else{
            str.append("&nbsp;<a href='#' disabled='true'>末页</a>");            
        }
        
        str.append("&nbsp;共" + pageCount + "页" + getCount() + "条记录");
        
        
        // 设置每页显示条数
        str.append("&nbsp;每页显示<SELECT size=1 name=pagesize onchange='pageNeeded.value=1;pageSize.value=this.value;submit();'>");
        
        if (pageSize == 3){
        	str.append("<OPTION value=3 selected>3</OPTION>");
        }else{
        	str.append("<OPTION value=3>3</OPTION>");
        }
        
        if (pageSize == 10){
        	str.append("<OPTION value=10 selected>10</OPTION>");
        }else{
        	str.append("<OPTION value=10>10</OPTION>");
        }
        
        if (pageSize == 20){
        	str.append("<OPTION value=20 selected>20</OPTION>");
        }else{
        	str.append("<OPTION value=20>20</OPTION>");
        }
        
        if (pageSize == 50){
        	str.append("<OPTION value=50 selected>50</OPTION>");
        }else{
        	str.append("<OPTION value=50>50</OPTION>");
        }
        
        if (pageSize == 100){
        	str.append("<OPTION value=100 selected>100</OPTION>");
        }else{
        	str.append("<OPTION value=100>100</OPTION>");
        }
        
        str.append("</SELECT>");
        str.append("条");

        // 设置跳转到第几页
        str.append("&nbsp;转到<SELECT size=1 name=Pagelist onchange='pageNeeded.value=this.value;submit();'>");
        for (int i = 1; i < pageCount + 1; i++){
            if (i == pageNeeded){
                str.append("<OPTION value=" + i + " selected>" + i + "</OPTION>");
            }else{
                str.append("<OPTION value=" + i + ">" + i + "</OPTION>");                
            }
        }
        str.append("</SELECT>页");
        
        str.append("<INPUT type=hidden  value=" + pageNeeded + " name='pageNeeded' > ");
        str.append("<INPUT type=hidden  value=" + pageSize + " name='pageSize' > ");

        return str.toString();
    }

    public int getFlag(){
        return flag;
    }

    public void setFlag(int flag){
        this.flag = flag;
    }

    public int getCountFilter(){
        return countFilter;
    }

    public void setCountFilter(int countFilter){
        this.countFilter = countFilter;
    }
    
    public PageListData(){
        newDataArray();
    }

    public List getDataArray(){
        return dataArray;
    }

    public void setDataArray(List dataArray){
        this.dataArray = dataArray;
    }

    public Object getData(int i){
        return getDataArray().get(i);
    }

    public void newDataArray(){
        if (dataArray == null){
            dataArray = new ArrayList();
        }
    }

    public void clearDataArray(){
        if (dataArray != null){
            dataArray.clear();
        }
    }

    public int getCount(){
        return count;
    }

    public void setCount(int count){
        if (pageSize != 0){
            pageCount = count / pageSize;
            if (count % pageSize != 0){
                pageCount++;
            }
        }
        this.count = count;
    }

    public int getPageCount(){
        return pageCount;
    }

    public void setPageCount(int pageCount){
        this.pageCount = pageCount;
    }

    public int getPageSize(){
        return pageSize;
    }

    public void setPageSize(int pageSize){
        this.pageSize = pageSize;
    }

    public int getPageNeeded(){
        return pageNeeded;
    }

    public void setPageNeeded(int pages){
        this.pageNeeded = pages;
    }    
}